package com.mku.wms_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mku.wms_service.common.QueryPageParam;
import com.mku.wms_service.common.Response;
import com.mku.wms_service.entity.Goods;
import com.mku.wms_service.mapper.GoodsMapper;
import com.mku.wms_service.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author maoxx
 * @since 2024-11-22
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Override
    public Response<Page<Goods>> listPage(QueryPageParam qp2) {
        Page<Goods> page = new Page<>();
        page.setCurrent(qp2.getPageNum()).setSize(qp2.getPageSize());
        QueryWrapper<Goods> qw = new QueryWrapper<>();

        String name = (String) qp2.getParam().get("name");
        Integer storage = (Integer) qp2.getParam().get("storage");
        Integer goodstype = (Integer) qp2.getParam().get("goodstype");

        if (name != null && !"".equals(name.trim())) {
            qw.like("name", name);
        }

        if (storage != null && storage >= 0) {
            qw.eq("storage", storage);
        }

        if (goodstype != null && goodstype > 0) {
            qw.eq("goodstype", goodstype);
        }

        Page<Goods> pages = page(page, qw);

        return Response.success(pages);
    }

    @Override
    public Response<Boolean> add(Goods goods) {
        return saveOrUpdate(goods) ? Response.success(true) : Response.fail("操作失败");
    }

    @Override
    public Response<Boolean> delete(Integer id) {
        return removeById(id) ? Response.success(true) : Response.fail("操作失败");
    }
}
